package com.sunmouse.toollib.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.StyleRes;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.sunmouse.toollib.R;


/**
 * Created by Kvin on 2017/1/20.
 */
public class PopupUtil extends PopupWindow {

    private static final int DEFAULT_RES = -1;

    private PopupUtil(Context context, PopupUtil.Builder builder) {
        super(builder.getView(), builder.getWidth(), builder.getHeight());
        this.setClippingEnabled(true);
        this.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setOutsideTouchable(true);
        this.setFocusable(true);
//        this.setAnimationStyle(builder.getAnim());
    }

    public static Builder init() {
        return new Builder();
    }

    /**
     * Builder
     */
    public static class Builder {
        private int width;
        private int height;
        private View v;
        private AnimStyle anim;

        public Builder() {
            this.anim=AnimStyle.BOTTOM_IN_BOTTOM_OUT;
        }

        public Builder view(View view) {
            this.v = view;
            return this;
        }

        public Builder width(int width) {
            this.width = width;
            return this;
        }

        public Builder height(int height) {
            this.height = height;
            return this;
        }

        public Builder anim(AnimStyle anim) {
            this.anim = anim;
            return this;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }

        public View getView() {
            return v;
        }

        public AnimStyle getAnim() {
            return anim;
        }

        public PopupUtil build(Context context) {

            return new PopupUtil(context, this);
        }
    }

    /**
     * set animation style
     */
    public void setAnimationStyle(@StyleRes int animationStyle) {
        this.setAnimationStyle(animationStyle);
    }

    public void setAnimationStyle(AnimStyle style) {
        switch (style) {
            case LEFT_IN_LEFT_OUT:
                this.setAnimationStyle(R.style.Transition_LeftInLeftOut);
                break;
            case RIGHT_IN_RIGHT_OUT:
                this.setAnimationStyle(R.style.Transition_RightInRightOut);
                break;
            case TOP_IN_TOP_OUT:
                this.setAnimationStyle(R.style.Transition_TopInTopOut);
                break;
            case BOTTOM_IN_BOTTOM_OUT:
                this.setAnimationStyle(R.style.Transition_BottomInBottomOut);
                break;
        }
    }

    /**
     * animation style
     */
    public enum AnimStyle {
        RIGHT_IN_RIGHT_OUT(0),
        LEFT_IN_LEFT_OUT(1),
        TOP_IN_TOP_OUT(2),
        BOTTOM_IN_BOTTOM_OUT(3);

        private int value;

        AnimStyle(int value) {
            this.value = value;
        }
    }
}